package com.xea.action;

import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.xea.model.News;
import com.xea.service.NewsService;
import com.xea.service.ServiceFactory;
import com.xea.util.StringUtil;

public class NewsAction extends DispatcherServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1932405126946046616L;
	
	
	/**
	 * 查询新闻
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void search(HttpServletRequest request,HttpServletResponse response) throws Exception{
		NewsService newsDao = ServiceFactory.getNewsService();
		String title=request.getParameter("title");
		if(StringUtil.isNullOrEmpty(title))
			title="";
		String pageNo=request.getParameter("pageNo");
		if(StringUtil.isNullOrEmpty(pageNo))
			pageNo="";
		request.setAttribute("pagedata", newsDao.search(title, pageNo));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/news/list.jsp");
		dispatcher.forward(request, response);	
	}
	
	/**
	 * 查询发布新闻
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void searchforout(HttpServletRequest request,HttpServletResponse response) throws Exception{
		NewsService newsDao = ServiceFactory.getNewsService();
		String pageNo=request.getParameter("pageNo");
		if(StringUtil.isNullOrEmpty(pageNo))
			pageNo="";
		request.setAttribute("pagedata", newsDao.searchforout(pageNo));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("outer/news/list.jsp");
		dispatcher.forward(request, response);	
	}
	
	/**
	 * 新增新闻
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void add(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		NewsService newsDao = ServiceFactory.getNewsService();
		response.setCharacterEncoding("GBK"); 
        String title=request.getParameter("title");
        String content=request.getParameter("content");
        String pub=request.getParameter("pub");
        newsDao.add(new News(title, content, new Date(),Integer.valueOf(pub)));
	}
	
	/**
	 * 查看新闻详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void findById(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		NewsService newsDao = ServiceFactory.getNewsService();
		String id=request.getParameter("id");
		request.setAttribute("news", newsDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher(request.getParameter("_url"));
		dispatcher.forward(request, response);
	}
	
	/**
	 * 查看新闻详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void detail(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		NewsService newsDao = ServiceFactory.getNewsService();
		String id=request.getParameter("id");
		request.setAttribute("news", newsDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/news/detail.jsp");
		dispatcher.forward(request, response);
	}
	
	/**
	 * 删除新闻
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void del(HttpServletRequest request,HttpServletResponse response) throws IOException{
		NewsService newsDao = ServiceFactory.getNewsService();
		String id=request.getParameter("id");
		newsDao.del(id);
	}
	
	/**
	 * 修改新闻
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		NewsService newsDao = ServiceFactory.getNewsService();
		String id=request.getParameter("id");
		String title=request.getParameter("title");
        String content=request.getParameter("content");
        String pub=request.getParameter("pub");
		response.setCharacterEncoding("utf-8");
        News news=newsDao.findById(Integer.valueOf(id));
        news.setTitle(title);
        news.setContent(content);
        news.setPub(Integer.valueOf(pub));
        newsDao.update(news);
	}
	/**
	 * 公开新闻
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void pub(HttpServletRequest request,HttpServletResponse response) throws IOException{
		NewsService newsDao = ServiceFactory.getNewsService();
		String id=request.getParameter("id");
		String pub=request.getParameter("pub");
		newsDao.pub(id,Integer.valueOf(pub));
	}
	
}
